﻿package cn.antscript.model 
{
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import cn.antscript.airCMS.data.singleData.ImgData;
	import cn.antscript.airCMS.data.UserData;
	import com.maclema.mysql.Connection;
	import com.maclema.mysql.events.MySqlErrorEvent;
	import com.maclema.mysql.events.MySqlEvent;
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.Statement;
	import flash.events.Event;
	/**
	 * ...
	 * @author 
	 */
	public class SqlModel extends MySqlToken
	{
		
		private var _connect:Connection;
		private var _sql:String;
		
		private var _completeFunction:Function;
		
		private static var _instance:SqlModel;
		
		private var _tempData:AbstractData;
		
		private var _password:String;
		
		public function SqlModel(privateClass:PrivateClass) 
		{
			
		}
		
		public static function getInstance():SqlModel {
			if (_instance == null) {
				_instance = new SqlModel(new PrivateClass);
			}
			return _instance;
			
		}
		
		//=================================login
		public function login(completeFunction:Function):void {
			
			
			_completeFunction = completeFunction;
			
			_sql = "select * from user where user='"+UserData.USER+"' AND password='"+UserData.PASSWORD+"'";
			_connect = new Connection(UserData.DB_HOST, UserData.DB_PORT, UserData.DB_USER, UserData.DB_PASSWORD, UserData.DB_DATABASE);
			_connect.addEventListener(Event.CONNECT, onLoginContented);
			_connect.addEventListener(MySqlErrorEvent.SQL_ERROR, onLoginErr);
			_connect.connect();
		}
		private function onLoginErr(e:MySqlErrorEvent):void 
		{
			_connect.disconnect();
			
			_connect.removeEventListener(Event.CONNECT, onLoginContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onLoginErr);			
			
			_completeFunction(false);
			_completeFunction = null;
		}
		private function onLoginContented(e:Event):void 
		{
			_connect.removeEventListener(Event.CONNECT, onLoginContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onLoginErr);
			
			var st:Statement = _connect.createStatement();        
			var token:MySqlToken = st.executeQuery(_sql);
			token.addEventListener(MySqlEvent.RESULT, onLoginResult);
			token.addEventListener(MySqlErrorEvent.SQL_ERROR, onLoginErr);
			
		}	
		
		private function onLoginResult(e:MySqlEvent):void 
		{
			e.target.removeEventListener(MySqlEvent.RESULT, onLoginResult);
			e.target.removeEventListener(MySqlErrorEvent.SQL_ERROR, onLoginErr);
			
			if (e.resultSet.next()) {
				_completeFunction(true);
			}
			else {
				_completeFunction(false);
			}
			 
			 _connect.disconnect();
			 
			_completeFunction = null;
			
			
		}
		//==========================================
		
		//=================================change password
		public function changePass(password:String,completeFunction:Function):void {
			
			_password = password;
			_completeFunction = completeFunction;
			
			_sql = "update user set password='" + password + "' where user='" + UserData.USER+"'";
			_connect = new Connection(UserData.DB_HOST, UserData.DB_PORT, UserData.DB_USER, UserData.DB_PASSWORD, UserData.DB_DATABASE);
			_connect.addEventListener(Event.CONNECT, onChangeContented);
			_connect.addEventListener(MySqlErrorEvent.SQL_ERROR, onChangeErr);
			_connect.connect();
		}
		private function onChangeErr(e:MySqlErrorEvent):void 
		{
			_connect.disconnect();
			
			_connect.removeEventListener(Event.CONNECT, onChangeContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onChangeErr);			
			
			_completeFunction(false);
			_completeFunction = null;
		}
		private function onChangeContented(e:Event):void 
		{
			_connect.removeEventListener(Event.CONNECT, onChangeContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onChangeErr);
			
			var st:Statement = _connect.createStatement();        
			var token:MySqlToken = st.executeQuery(_sql);
			token.addEventListener(MySqlEvent.RESPONSE, onChangeResult);
			token.addEventListener(MySqlErrorEvent.SQL_ERROR, onChangeErr);
		}	
		
		private function onChangeResult(e:MySqlEvent):void 
		{
			e.target.removeEventListener(MySqlEvent.RESULT, onChangeResult);
			e.target.removeEventListener(MySqlErrorEvent.SQL_ERROR, onChangeErr);
			
			_completeFunction(true);			
			_completeFunction = null;
			
			_connect.disconnect();
			 
		}
		
		
		//==========================================
		
		
		//============== Insert
		public function add(data:AbstractData,group:String,pageID:String,completeFunction:Function):void {
			
			_completeFunction = completeFunction;
			
			_sql = "INSERT INTO " + data.table + "(label,pageID,content,containerLabel) VALUES ('" + data.label + "',"+pageID+",'" + data.txt + "','" + group + "')";
			_connect = new Connection(UserData.DB_HOST, UserData.DB_PORT, UserData.DB_USER, UserData.DB_PASSWORD, UserData.DB_DATABASE);
			_connect.addEventListener(Event.CONNECT, onAddContented);
			_connect.addEventListener(MySqlErrorEvent.SQL_ERROR, onAddErr);
			_connect.connect();trace(_sql)
		}
		
		private function onAddErr(e:MySqlErrorEvent):void 
		{
			_connect.disconnect();
			
			_connect.removeEventListener(Event.CONNECT, onAddContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onAddErr);
			
			_completeFunction();
			_completeFunction = null;
		}
		
		private function onAddContented(e:Event):void 
		{
			_connect.removeEventListener(Event.CONNECT, onAddContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onAddErr);
			
			var st:Statement = _connect.createStatement();        
			var token:MySqlToken = st.executeQuery(_sql);
			token.addEventListener(MySqlEvent.RESPONSE, onAddResponse);
			token.addEventListener(MySqlErrorEvent.SQL_ERROR, onAddErr);
		}
		
		private function onAddResponse(e:MySqlEvent):void 
		{
			e.target.addEventListener(MySqlEvent.RESPONSE, onAddResponse);
			e.target.addEventListener(MySqlErrorEvent.SQL_ERROR, onAddErr);
			
			_connect.disconnect();
			var id:String = e.insertID.toString();
			if (e.affectedRows > 0) {
				_completeFunction(id);
				_completeFunction = null;
			}
			
		}
		//========================================================
		
		//============== UPDATE URL
		public function updateURL(data:AbstractData, completeFunction:Function):void {
			
			_completeFunction = completeFunction;
			
			if (data is ImgData) {
				_sql = "update " + data.table + " set url='" + (data as ImgData).url + "',thumb='" + (data as ImgData).thumb + "' where id=" + data.id;
			}
			else {
				_sql = "update " + data.table + " set url='" + data["url"] + "' where id=" + data.id;
			}
			
			
			_connect = new Connection(UserData.DB_HOST, UserData.DB_PORT, UserData.DB_USER, UserData.DB_PASSWORD, UserData.DB_DATABASE);
			_connect.addEventListener(Event.CONNECT, onUpdateURLContented);
			_connect.addEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateURLErr);
			_connect.connect();
		}
		
		private function onUpdateURLErr(e:MySqlErrorEvent):void 
		{
			_connect.disconnect();
			
			_connect.removeEventListener(Event.CONNECT, onUpdateURLContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateURLErr);
			
			_completeFunction();
			_completeFunction = null;
		}
		
		private function onUpdateURLContented(e:Event):void 
		{
			_connect.removeEventListener(Event.CONNECT, onUpdateURLContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateURLErr);
			
			var st:Statement = _connect.createStatement();        
			var token:MySqlToken = st.executeQuery(_sql);
			token.addEventListener(MySqlEvent.RESPONSE, onUpdateURLResponse);
			token.addEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateURLErr);
		}
		
		private function onUpdateURLResponse(e:MySqlEvent):void 
		{
			e.target.addEventListener(MySqlEvent.RESPONSE, onUpdateURLResponse);
			e.target.addEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateURLErr);
			
			_connect.disconnect();
			
			if (e.affectedRows > 0) {
				_completeFunction();
				_completeFunction = null;
			}
			
		}
		//========================================================
		
		
		//============== UPDATE 
		public function update(data:AbstractData, completeFunction:Function):void {
			
			_completeFunction = completeFunction;
			
			_sql = "update " + data.table + " set label='" + data.label + "',content='" + data.txt + "' where id=" + data.id;
			_connect = new Connection(UserData.DB_HOST, UserData.DB_PORT, UserData.DB_USER, UserData.DB_PASSWORD, UserData.DB_DATABASE);
			_connect.addEventListener(Event.CONNECT, onUpdateContented);
			_connect.addEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateErr);
			_connect.connect();
		}
		
		private function onUpdateErr(e:MySqlErrorEvent):void 
		{
			_connect.disconnect();
			
			_connect.removeEventListener(Event.CONNECT, onUpdateContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateErr);
			
			_completeFunction();
			_completeFunction = null;
		}
		
		private function onUpdateContented(e:Event):void 
		{
			_connect.removeEventListener(Event.CONNECT, onUpdateContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateErr);
			
			var st:Statement = _connect.createStatement();        
			var token:MySqlToken = st.executeQuery(_sql);
			token.addEventListener(MySqlEvent.RESPONSE, onUpdateResponse);
			token.addEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateErr);
		}
		
		private function onUpdateResponse(e:MySqlEvent):void 
		{
			e.target.addEventListener(MySqlEvent.RESPONSE, onUpdateResponse);
			e.target.addEventListener(MySqlErrorEvent.SQL_ERROR, onUpdateErr);
			
			_connect.disconnect();
			
			if (e.affectedRows > 0) {
				_completeFunction();
				_completeFunction = null;
			}
			
		}
		//========================================================
		
		//============== DELETE 
		public function del(data:AbstractData, completeFunction:Function):void {
			
			_completeFunction = completeFunction;
			
			_sql = "DELETE FROM " + data.table + " where id=" + data.id;
			_connect = new Connection(UserData.DB_HOST, UserData.DB_PORT, UserData.DB_USER, UserData.DB_PASSWORD, UserData.DB_DATABASE);
			_connect.addEventListener(Event.CONNECT, onDelContented);
			_connect.addEventListener(MySqlErrorEvent.SQL_ERROR, onDelErr);
			_connect.connect();
		}
		
		private function onDelErr(e:MySqlErrorEvent):void 
		{
			_connect.disconnect();
			
			_connect.removeEventListener(Event.CONNECT, onDelContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onDelErr);
			
			_completeFunction();
			_completeFunction = null;
		}
		
		private function onDelContented(e:Event):void 
		{
			_connect.removeEventListener(Event.CONNECT, onDelContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onDelErr);
			
			var st:Statement = _connect.createStatement();        
			var token:MySqlToken = st.executeQuery(_sql);
			token.addEventListener(MySqlEvent.RESPONSE, onDelResponse);
			token.addEventListener(MySqlErrorEvent.SQL_ERROR, onDelErr);
		}
		
		private function onDelResponse(e:MySqlEvent):void 
		{
			e.target.addEventListener(MySqlEvent.RESPONSE, onDelResponse);
			e.target.addEventListener(MySqlErrorEvent.SQL_ERROR, onDelErr);
			e.target.addEventListener(MySqlErrorEvent.SQL_ERROR, onDelErr);
			
			_connect.disconnect();
			
			if (e.affectedRows > 0) {
				_completeFunction();
				_completeFunction = null;
			}
			
		}
		//========================================================
		
		
	}

}

class PrivateClass {
	
}